<div class="row">
	<div class="col-md-12">
		<form id="addForm" class="form-horizontal" action="${request.contextPath}/cms/ad/category/add" method="post" onsubmit="return false">
			<div class="box-body" >
				<div class="form-group">
					<label id="adCategorySiteIdLabel" for="adCategorySiteId" class="col-sm-2  control-label">所属站点</label>
					<div class="col-sm-10">
						<select class="form-control" id="adCategorySiteId"  name="siteId" style="width: 100%;" placeholder="选择站点...">
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="adCategoryNameLabel" for="adCategoryName" class="col-sm-2 control-label">名称</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="name" id="adCategoryName" placeholder="输入名称...">
					</div>
					<label id="adCategoryCodeLabel" for="adCategoryCode" class="col-sm-2 control-label">编码</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="code" id="adCategoryCode" placeholder="输入编码...">
					</div>
				</div>
				<div class="form-group">
					<label id="adCategoryDescriptionLabel" class="col-sm-2 control-label" >描述</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" name="description" id="adCategoryDescription" placeholder="输入描述...">
					</div>
				</div>
				<div class="form-group">
					<label id="adCategorySortOrderLabel" for="adCategorySortOrder" class="col-sm-2  control-label">排序</label>
					<div class="col-sm-2">
						<input  class="form-control" name="sortOrder" id="adCategorySortOrder" type="number" placeholder="请输入...">
					</div>
					<label id="adCategoryStatusLabel" for="adCategoryStatus" class="col-sm-2  control-label">状态</label>
					<div class="col-sm-2">
						<select class="form-control" id="adCategoryStatus"  name="status" style="width: 100%;">
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="adCategoryRemarkLabel" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" name="remark" id="adCategoryRemark" value="${cmsAdCategory.remark}" placeholder="输入备注...">
					</div>
				</div>
				<div class="error-message text-center" id="addFormErrorMsgContainer">
					<span class="error-icon"></span>
					<span id="addFormErrorMsg" class="error-msg" ></span>
				</div>
			</div>
			<div class="box-body">
				<div class="pull-right">
					<button type="submit" class="btn btn-primary btn-sm" ><i class="fa fa-save"></i> 保存</button>
					<button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><i class="fa fa-close"></i> 取消</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
function ajaxSubmit(form) {

	const $form = $(form);

	if ($form.length === 1 && $form.validate()) {
		$.post(form.action, $form.serialize(),
			function(result) {
				if (result.success) {
					$("#lgModal").modal('hide');
		        	alertMsg(result.message,"success");
					dataGridReload();
				} else {
					$form.find(".error-msg").show();
					$form.find(".error-msg").html(result.message);
				}
			}, 'json');
	}
}

$(function() {

    $("#adCategoryStatus").select2({
        data: StatusStore,
        theme: "bootstrap",
        minimumResultsForSearch: Infinity
    });

    //初始化时间选择器
    $('[name=startTime]').datetimepicker({
        format:"Y/m/d 00:00:00",      //格式化日期
        timepicker:true,    //开启时间选项
        yearStart:2000,     //设置最小年份
        yearEnd:2050,        //设置最大年份
        todayButton:false    //关闭选择今天按钮
    });

    //初始化时间选择器
    $('[name=endTime]').datetimepicker({
        format:"Y/m/d 23:59:59",      //格式化日期
        timepicker:true,    //开启时间选项
        yearStart:2000,     //设置最小年份
        yearEnd:2050,        //设置最大年份
        todayButton:false    //关闭选择今天按钮
    });

	$.ajax({
		type:"get",
		url:'/cms/site/list',
		dataType:'json'
	}).done(function(result){
		var o = [];
		var a = result;
		$.each(a, function() {
			if(this.id !== undefined && this.id !==''){
				o.push({'id':this.id,'text':this.name});
			}
		});
		$("#adCategorySiteId").select2({
			data:o,
			placeholder: "站点...",
			allowClear: true,
			language: "zh-CN",
			theme: "bootstrap"
		}).val('').trigger("change");
	});


	var $addForm = $("#addForm");
	$addForm.validate({
		rules: {
            name: {
				required: true
			},
            sortOrder: {
				required: true
			},
            status: {
				required: true
			}
		},
		messages: {
            name:{
				required:"名称不能为空！"
			},
			sortOrder:{
				required:"排序不能为空！"
			},
            status:{
				required:"状态不能为空！"
			}
		},
		submitHandler:ajaxSubmit,
		errorLabelContainer:"#addFormErrorMsgContainer",
		wrapper:"li",
		showErrors:function(errorMap,errorList) {
			if(errorList.length>0){
				$addForm.find(".error-msg").html(errorList[0].message);
			}else{
				$addForm.find(".error-msg").html("");
			}
		},
		onkeyup: false
	});
});
</script>